import axios from 'axios'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
import {getuserTempId} from '@/utils/userTempId'

// console.log(process.env,'process');

// 创建一个实例
const request = axios.create({
    baseURL: process.env.VUE_APP_API,
    timeout: 10000
})

// 设置请求拦截器
request.interceptors.request.use(
    // 处理成功的promise实例
    (config) => { 
        nProgress.start()
       const userTempId = getuserTempId()
       const token = store.state.user.token;
    //    console.log(store,'store');
       config.headers.token = token ;
        config.headers.userTempId = userTempId;
        return config
    },
    (error) => {
        return Promise.reject(error)
     }
)

// 设置响应拦截器
request.interceptors.response.use(
    (response)=>{
        nProgress.done()
        // console.log(response.data.data,'response');
        if(response.data.code === 200){
            return response.data.data
        }else{
            
            return Promise.reject()
        }
        
    },
    (error)=>{
        nProgress.done()
        
        return Promise.reject(error)
    }
)

export default request

